SMS.service('player', function ($rootScope, $window, utils, globals, model, notifications) {
    var player1 = '#playdeck_1';
    var player2 = '#playdeck_2';
    var timerid = 0;

    $rootScope.defaultPlay = function (data, event) {
        if (typeof $(player1).data("jPlayer") == 'undefined') {
            $rootScope.nextTrack();
        }
    }
    $rootScope.nextTrack = function () {
        var next = getNextMediaElement();
        if (next) {
            $rootScope.playMediaElement(false, next);
        }
    }
    $rootScope.previousTrack = function () {
        var next = getNextMediaElement(true);
        if (next) {
            $rootScope.playMediaElement(false, next);
        }
    }
    getNextMediaElement = function (previous) {
        var mediaElement;
        if (globals.settings.Debug) { console.log('Getting Next Track > ' + 'Queue length: ' + $rootScope.queue.length); }
        if ($rootScope.queue.length > 0) {
            angular.forEach($rootScope.queue, function(item, key) {
                if (item.playing === true) {
                    mediaElement = item;
                }
            });
            var index = $rootScope.queue.indexOf(mediaElement);
            var next;
            if (previous) {
                next = $rootScope.queue[index - 1];
            } else {
                next = $rootScope.queue[index + 1];
            }
            if (typeof next != 'undefined') {
                if (globals.settings.Debug) { console.log('Next Track: ' + next.id); }
                return next;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    this.startSaveTrackPosition = function () {
        if (globals.settings.SaveTrackPosition) {
            if (timerid != 0) {
                clearInterval(timerid);
            }
            timerid = $window.setInterval(function () {
                if (globals.settings.SaveTrackPosition) {
                    saveTrackPosition();
                }
            }, 30000);
        }
    }
    saveTrackPosition = function () {
        var audio = $(player1).data("jPlayer");
        if (typeof audio != 'undefined') {
            if (audio.status.currentTime > 0 && audio.status.paused == false) {
                var mediaElement;
                angular.forEach($rootScope.queue, function(item, key) {
                    if (item.playing === true) {
                        mediaElement = item;
                    }
                });
                if (mediaElement) {
                    var position = audio.status.currentTime;
                    if (position != null) {
                        $('#action_SaveProgress').fadeOut("slow").delay(500).fadeIn("slow").delay(500).fadeOut("slow").delay(500).fadeIn("slow");
                        mediaElement.position = position;
                        // Save Queue
                        if (utils.browserStorageCheck()) {
                            try {
                                var mediaElementStr = angular.toJson(mediaElement);
                                localStorage.setItem('CurrentMediaElement', mediaElementStr);
                                if (globals.settings.Debug) { console.log('Saving Current Position: ' + mediaElementStr); }
                                var html = localStorage.getItem('CurrentQueue');
                                if ($rootScope.queue.length > 0) {
                                    var current = $rootScope.queue;
                                    if (current != html) {
                                        localStorage.setItem('CurrentQueue', angular.toJson(current));
                                        if (globals.settings.Debug) { console.log('Saving Queue: ' + current.length + ' characters'); }
                                    }
                                }
                            } catch (e) {
                                if (e == QUOTA_EXCEEDED_ERR) {
                                    alert('Quota exceeded!');
                                }
                            }
                        } else {
                            if (globals.settings.Debug) { console.log('HTML5::loadStorage not supported on your browser'); }
                        }
                    }
                }
            }
        }
    }
    this.loadTrackPosition = function () {
        if (utils.browserStorageCheck()) {
            // Load Saved Media Element
            var mediaElement = angular.fromJson(localStorage.getItem('CurrentMediaElement'));
            if (mediaElement) {
                $rootScope.playMediaElement(true, mediaElement);
                // Load Saved Queue
                var items = angular.fromJson(localStorage.getItem('CurrentQueue'));
                if (items) {
                    $rootScope.queue = items;
                    if ($rootScope.queue.length > 0) {
                        notifications.updateMessage($rootScope.queue.length + ' Saved Tracklist', true);
                    }
                    if (globals.settings.Debug) { console.log('Play Queue Loaded From localStorage: '); }
                }
            }
        } else {
            if (globals.settings.Debug) { console.log('HTML5::loadStorage not supported on your browser'); }
        }
    }
    deleteCurrentQueue = function (data) {
        if (utils.browserStorageCheck()) {
            localStorage.removeItem('CurrentQueue');
            utils.setValue('CurrentMediaElement', null, false);
            if (globals.settings.Debug) { console.log('Removing Play Queue'); }
        } else {
            if (globals.settings.Debug) { console.log('HTML5::loadStorage not supported on your browser, ' + html.length + ' characters'); }
        }
    };
    $rootScope.playMediaElement = function (loadonly, data) {
        if (globals.settings.Debug) { console.log('Play: ' + JSON.stringify(data, null, 2)); }
        angular.forEach($rootScope.queue, function(item, key) {
            item.playing = false;        
        });
        data.playing = true;
        data.selected = false;
        $rootScope.playingMediaElement = data;

        var id = data.id;
        var url = data.url;
        var position = data.position;
        var title = data.title;
        var album = data.album;
        var artist = data.artist;
        var suffix = data.suffix;
        var coverArtThumb = data.coverArtThumb;
        var coverArtFull = data.coverArtFull;
        $('#playermiddle').css('visibility', 'visible');
        $('#mediadetails').css('visibility', 'visible');

        $rootScope.loadjPlayer(player1, url, suffix, loadonly, position);
        if (!loadonly) {
            // Sway.fm UnityShim
            var playerState = {
                playing: true,
                title: title,
                artist: artist,
                favorite: false,
                albumArt: coverArtFull
            }
            if ($rootScope.unity) {
                $rootScope.unity.sendState(playerState);
            }
            // End UnityShim
        }
        $('#Queue').stop().scrollTo('#' + id, 400);
        $('#QueuePreviewList').stop().scrollTo('#' + id, 400);
        var spechtml = '';
        var data = $(player1).data().jPlayer;
        for (i = 0; i < data.solutions.length; i++) {
            var solution = data.solutions[i];
            if (data[solution].used) {
                spechtml += "<strong class=\"codesyntax\">" + solution + "</strong> is";
                spechtml += " currently being used with<strong>";
                for (format in data[solution].support) {
                    if (data[solution].support[format]) {
                        spechtml += " <strong class=\"codesyntax\">" + format + "</strong>";
                    }
                }
                spechtml += "</strong> support";
            }
        }
        $('#SMStats').html(spechtml);

        if (globals.settings.NotificationTrack && !loadonly) {
            notifications.showNotification(coverArtThumb, utils.toHTML.un(title), utils.toHTML.un(artist + ' - ' + album), 'text', '#NextTrack');
        }
        if (globals.settings.ScrollTitle) {
            var title = utils.toHTML.un(artist) + ' - ' + utils.toHTML.un(title);
            utils.scrollTitle(title);
        } else {
            utils.setTitle(utils.toHTML.un(artist) + ' - ' + utils.toHTML.un(title));
        }
        //utils.safeApply();
        if(!$rootScope.$root.$$phase) {
            $rootScope.$apply();
        }
    };
    $rootScope.loadjPlayer = function (el, url, suffix, loadonly, position) {
        // jPlayer Setup
        var volume = 1;
        if (utils.getValue('Volume')) {
            volume = parseFloat(utils.getValue('Volume'));
        }
        var audioSolution = "html,flash";
        if (utils.getValue('ForceFlash')) {
            audioSolution = "flash,html";
        }
        $(el).jPlayer("destroy");
        $.jPlayer.timeFormat.showHour = true; 
        $(el).jPlayer({
            swfPath: "js/plugins/jplayer",
            wmode: "window",
            solution: audioSolution,
            supplied: suffix,
            volume: volume,
            errorAlerts: false,
            warningAlerts: false,
            cssSelectorAncestor: "#player",
            cssSelector: {
                play: "#PlayTrack",
                pause: "#PauseTrack",
                seekBar: "#audiocontainer .scrubber",
                playBar: "#audiocontainer .progress",
                mute: "#action_Mute",
                unmute: "#action_UnMute",
                volumeMax: "#action_VolumeMax",
                currentTime: "#played",
                duration: "#duration"
            },
            ready: function () {
                if (suffix == 'oga') {
                    $(this).jPlayer("setMedia", {
                        oga: url,
                    });
                } else if (suffix == 'mp3') {
                    $(this).jPlayer("setMedia", {
                        mp3: url,
                    });
                }
                if (!loadonly) { // Start playing
                    $(this).jPlayer("play");
                } else { // Loadonly
                    $(this).jPlayer("pause", position);
                }
                if (globals.settings.Debug) {
                    console.log('[jPlayer Version Info]');
                    utils.logObjectProperties($(el).data("jPlayer").version);
                    console.log('[HTML5 Debug Info]');
                    utils.logObjectProperties($(el).data("jPlayer").html);
                    console.log('[Flash Debug Info]');
                    utils.logObjectProperties($(el).data("jPlayer").flash);
                    console.log('[jPlayer Options Info]');
                    utils.logObjectProperties($(el).data("jPlayer").options);
                }
		    },
            timeupdate: function(event) {
            },
            volumechange: function(event) {
                utils.setValue('Volume', event.jPlayer.options.volume, true);
            },
            ended: function(event) {
                if (globals.settings.Repeat) { // Repeat current track if enabled
                    $(this).jPlayer("play");
                } else {
                    if (!getNextMediaElement()) { // Action if we are at the last media element in queue
                        if (globals.settings.LoopQueue) { // Loop to first track in queue if enabled
                            var next = $rootScope.queue[0];
                            $rootScope.playMediaElement(false, next);
                        }
                    } else {
                        $rootScope.nextTrack();
                    }
                }
            },
            error: function(event) {
                var time = $(player1).data("jPlayer").status.currentTime;
                $(player1).jPlayer("play", time);
                if (globals.settings.Debug) { 
                    console.log("Error Type: " + event.jPlayer.error.type); 
                    console.log("Error Context: " + event.jPlayer.error.context); 
                    console.log("Error Message: " + event.jPlayer.error.message); 
                    console.log("Stream interrupted, retrying from position: " + time);
                }
            }
        });
        return;
    }
    this.playPause = function () {
        if (typeof $(player1).data("jPlayer") != 'undefined') {
            if ($(player1).data("jPlayer").status.paused) {
                $(player1).jPlayer("play");
            } else {
                $(player1).jPlayer("pause");
            }
        } 
    }
    playVideo = function (id, bitrate) {
        var w, h;
        bitrate = parseInt(bitrate);
        if (bitrate <= 600) { 
            w = 320; h = 240; 
        } else if (bitrate <= 1000) { 
            w = 480; h = 360; 
        } else { 
            w = 640; h = 480; 
        } 
        //$("#jPlayerSelector").jPlayer("option", "fullScreen", true);
        $("#videodeck").jPlayer({
		    ready: function () {
                /*
                $.fancybox({
                    autoSize: false,
                    width: w + 10,
                    height: h + 10,
                    content: $('#videodeck')
                });
                */
			    $(this).jPlayer("setMedia", {
				    m4v: 'https://&id=' + id + '&salt=83132'
			    }).jPlayer("play");
                $('#videooverlay').show();
		    },
		    swfPath: "js/jplayer",
		    solution: "html, flash",
		    supplied: "m4v"
	    });
    }
});